package com.di.base.controller;

import com.di.base.common.dto.StorageRequestDTO;
import com.di.base.common.exception.BusinessException;
import com.di.base.service.StorageTccService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/storage")
public class StorageTblController {

    @Autowired
    private StorageTccService storageTccService;

    @PostMapping("/decrease")
    public void decrease(@RequestBody StorageRequestDTO request) {
        String productId = request.getProductId();
        Integer count = request.getCount();
        try {
            storageTccService.decrease(productId, count);
        } catch (BusinessException e) {
            log.error("库存扣减失败 productId={}, count={}", productId, count, e);
            throw e;
        }
    }

    @PostMapping("/increase")
    public void increase(@RequestBody StorageRequestDTO request) {
        String productId = request.getProductId();
        Integer count = request.getCount();
        try {
            storageTccService.increase(productId, count);
        } catch (BusinessException e) {
            log.error("库存回滚失败 productId={}, count={}", productId, count, e);
            throw e;
        }
    }
    @PostMapping("/test")
    public void test(@RequestBody StorageRequestDTO request) {
        String productId = request.getProductId();
        Integer count = request.getCount();
        log.info("test:成功调用:{},{}",productId,count);
    }
}